<template>
  <a-modal
    title="新增单表业务"
    width="80%"
    :destroyOnClose="true"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="名称"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              has-feedback
            >
              <a-input placeholder="请输入名称" v-decorator="['name', {rules: [{required: true, message: '请输入名称！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="编码"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              has-feedback
            >
              <a-input placeholder="请输入编码（唯一性）" v-decorator="['code', {rules: [{required: true, message: '请输入编码（唯一性）！'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="状态"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <a-radio-group placeholder="请选择状态" v-decorator="['state',{/*initialValue: initState,*/ rules: [{ required: true, message: '请选择状态（0：冻结；1：正常）！' }]}]" >
                <a-radio v-for="(item,index) in stateData" :key="index" :value="item.code">{{ item.name }}</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="类型"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <a-select style="width: 100%" placeholder="请选择类型" v-decorator="['type',{rules: [{ required: true, message: '请选择类型！' }]}]">
                <a-select-option v-for="(item,index) in typeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="所属机构"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              extra="不填则默认为当前用户的所属机构"
            >
              <a-tree-select
                v-decorator="['orgId', {rules: [{ required: true, message: '请选择机构！' }]}]"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                :treeData="orgTree"
                placeholder="请选择机构"
                treeDefaultExpandAll
              >
              </a-tree-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="负责人"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <user-select
                placeholder="请选择负责人"
                v-decorator="['ponPer', { rules: [{ required: false, message: '请选择负责人' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="相关人"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <user-select
                mode="multiple"
                placeholder="请选择相关人"
                v-decorator="['relPer', { rules: [{ required: false, message: '请选择相关人' }] }]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <custom-upload
              ref="enclosure"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              :isDisabled="false"
              labelName="附件"
              colName="enclosure"
              :rules="[{required: false, message: ''}]" />
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <a-form-item
              label="备注信息"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-textarea :rows="3" placeholder="请输入备注信息" v-decorator="['postscript', {rules: [{ required: false, message: '请选择机构！' }]}]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { simpleAdd } from '@/api/modular/main/simple/simpleManage'
  import { getOrgTree } from '@/api/modular/system/orgManage'
  import UserSelect from '@/components/MyComp/UserSelect'
  import CustomUpload from '@/components/MyComp/CustomUpload'
  import { STATE_DIC, STATE } from '@/consts/simple'
  import { mapGetters } from 'vuex'
  export default {
    components: {
      UserSelect,
      CustomUpload
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        labelCol_T: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol_T: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        // initState: STATE.NORMAL,
        stateData: STATE_DIC,
        typeData: [],
        orgTree: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        // 從緩存中獲取字典項
        this.typeData = this.$options.filters['dictData']('bis_type')
        // 取数据
        getOrgTree().then((res) => {
          this.orgTree = res.data
        })
        // 初始化赋值
        this.form.getFieldDecorator('state', { initialValue: STATE.FREEZE })
        // 判断当前用户是否是超级管理员
        if (this.userInfo?.adminType !== 1) {
          this.form.getFieldDecorator('ponPer', { initialValue: this.userInfo?.id })
          this.form.getFieldDecorator('orgId', { initialValue: this.userInfo?.loginEmpInfo?.orgId })
        }

        // 设置值
        // this.$nextTick(() => {
        //   this.form.setFieldsValue({
        //     state: STATE.OTHER
        //   })
        // })
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        this.form.setFieldsValue({
          enclosure: this.$refs.enclosure.getFileList()
        })
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            // 進行數據的二次處理
            if (values.relPer) {
              values.relPer = values.relPer.join(';')
            }
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            // 進行數據的二次處理
            simpleAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败：' + res.message)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        // 取消是文件要单独置空
        this.$refs.enclosure.setFileList(null)
        // 表單的字段内容進行重置
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
